package br.com.felinelayer.client.clientSwing;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;
import java.awt.Container;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;

/**
 *
 */
public class Frame extends ComponentSwing<Frame, JFrame> implements br.com.felinelayer.client.IFrame, WindowListener {

    private JFrame frame;
    private Container contentPane;

    @Override
    protected void doInitialize() {

        // Create the frame
        String title = "Frame Title";
        frame = new JFrame(title);
        //        // Create a component to add to the frame
        //        JComponent comp = new JTextArea();
        //        // Add the component to the frame's content pane;
        //        // by default, the content pane has a border layout
        //        frame.getContentPane().add( comp, BorderLayout.CENTER );
        // Show the frame
        int width = 600;
        int height = 300;
        // 
        frame.addWindowListener(this);
        // 
        contentPane = frame.getContentPane();
        contentPane.setLayout(null);
        //contentPane.setLayout( new FlowLayout( FlowLayout.CENTER ) );
        frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(false);
    }

//    private JPanel rowPanel;    
//    private JPanel addRowPanel() {
//        rowPanel = new JPanel();
//        rowPanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
//        contentPane.add( rowPanel );                
//        return rowPanel;
//    }
    @Override
    public void addComponent(br.com.felinelayer.client.IComponent component) {
        // 
        if (component.getObject() instanceof java.awt.Component) {
            java.awt.Component o = (java.awt.Component) component.getObject();
            contentPane.add(o);
            //rowPanel.add( o );
        }
    }

    @Override
    public Object getObject() {
        return frame;
    }

    @Override
    public Frame setText(String text) {
        frame.setTitle(text);
        return this;
    }

    @Override
    public void close() {
        frame.setVisible(false);
    }

    @Override
    public void windowClosing(WindowEvent e) {
        Runnable runner = new Runnable() {
            @Override
            public void run() {
                try {
                    Message message = createEvent(Protocol.EVENT_BEFORE_CLOSE);
                    message.setValueByName(Protocol.EVENT_BEFORE_CLOSE_CANCLOSE, Protocol.YES);
                    message = fireInquireEvent(message);

                    //fireEvent(Protocol.EVENT_CLICK);

                    if (message.getValueByName(Protocol.EVENT_BEFORE_CLOSE_CANCLOSE).equals(
                            Protocol.YES)) {
                        frame.setVisible(false);
                        System.exit(0);
                    }
                } catch (Exception ex) {
                    Logger.getLogger(Frame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };
        new Thread(runner).start();

        //SwingUtilities.invokeLater( runner );
        //TODO: mandar mensagem de fechar 
        // before (permitir impedir fechamento) e after
        // TODO: adicionar o evento de finalização, igual ao feito do rech.Log

    }

    @Override
    public void windowOpened(WindowEvent e) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowClosed(WindowEvent e) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowIconified(WindowEvent e) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowActivated(WindowEvent e) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
        //throw new UnsupportedOperationException( "Not supported yet." );
    }

    @Override
    public String getText() {
        return frame.getTitle();
    }

//    @Override
//    public Frame setVisible(boolean visible ) {
//        frame.setVisible( visible );
//        return this;
//    }
    @Override
    public void messageReceived(Message message) {
        if (message.getName().equalsIgnoreCase(Protocol.MSG_NAME_METHOD)) {
            String method = message.getValueByName(Protocol.METHOD_NAME);
            if (method.equalsIgnoreCase(Protocol.METHOD_FRAME_SHOW)) {
                frame.setVisible(true);
            }
            if (method.equalsIgnoreCase(Protocol.METHOD_FRAME_CLOSE)) {
                frame.setVisible(false);
            }
        }
    }
}
